<?php

include_once 'Constants.php';

class User extends Zend_Db_Table_Row_Abstract
{
    protected $_name = 'users';

    // mini-factory to generate Administrator or FuneralDirector objects
    // from a User object.
    public function resolveToObject()
    {
        if($this->isKeystoneAdmin())
        {
            $admin = new Administrator();
            $admin->makeFromUser($this);
            return $admin;
        }

        if($this->isFuneralDirector() || $this->isFuneralHomeAdmin())
        {
            $fd = new FuneralDirector();
            $fd->makeFromUser($this);
            return $fd;
        }
        
        return null;
    }

    // convenience methods
    public function isFuneralDirector()
    {
        return $this->role_name == FUNERAL_DIRECTOR;
    }

    public function isKeystoneAdmin()
    {
        return $this->role_name == KEYSTONE_ADMIN;
    }

    public function isFuneralHomeAdmin()
    {
        return $this->role_name == FUNERALHOME_ADMIN;
    }

    // gives/removes admin privs w.r.t. funeral director.
    // should check to see if FH already has an admin and
    // if they can have more than one.
    public function makeFuneralHomeAdmin( $yesNo )
    {
        // this really just resets the role name.  if $yesNo == "YES"
        // then sets the role name to FUNERALHOME_ADMIN.  if $yesNo == "NO"
        // then sets the role name to FUNERAL_DIRECTOR.  then it saves
        // the record.
    }

    // user role and permissions methods
    public function getRole()
    {
        return $this->role_name;
    }

    public function setRole( $role )
    {
        $user_roles = new UserRoles();
        if($user->roles->role_exists($role))
            $this->user_role = $role;
    }

    public function getPermissions()
    {
        $permsArray = array();
        $user_permissions = new UserPermissions();
        $perms = $user_permissions->findAll("user_role_name='{$this->role_name}'");
        foreach( $perms as $perm )
        {
            $op = $perm->operation;
            $target = $perm->operation_target;
            $params = $perm->operation_params;
            $permsArray[] = array('operation'=>$op, 'target'=>$target, 'params'=>$params );
        }
        return $permsArray;
    }
}

?>
